package com.teach.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teach.commons.Id;
import com.teach.dto.CommentsSearch;
import com.teach.entity.Comments;
import com.teach.exception.CodeException;
import com.teach.mapper.CommentsMapper;
import com.teach.service.CommentsService;
/**
 * <pre>
 * 评论业务逻辑实现类
 * 创建时间 ：     2019年12月13日 下午4:38:36
 * 当前版本 ：     V1.0
 * 版权 ：            舒星所有
 * </pre>
 * @author 舒星
 *
 */
@Service
public class CommentsServiceImpl implements CommentsService {
	@Autowired
	private CommentsMapper mapper;
	@Autowired
	private Id id;
	@Override
	public void commentsInsert(Comments comments) throws CodeException {
		// TODO Auto-generated method stub
		try {
			if(comments.getCommentId()==null) {
				comments.setCommentId(id.nextId());
			}
			comments.setCommentDelete(false);
			comments.setCommentTime(new Date());
			System.out.println(comments);
			mapper.insertSelective(comments);
		}catch(Exception e) {
			e.printStackTrace();
			throw new CodeException(5000);
		}
	}

	@Override
	public void commentsRemoveAll(Long commentCommentator) throws CodeException {
		// TODO Auto-generated method stub
		try {
			mapper.updateByCommentCommentator(commentCommentator);
		}catch(Exception e) {
			throw new CodeException(5001);
		}
	}

	@Override
	public void commentsRemove(String userName, Long commentCommentator) throws CodeException {
		// TODO Auto-generated method stub
		try {
			mapper.updateByCommentUserName(userName,commentCommentator);
		}catch(Exception e) {
			throw new CodeException(5001);
		}
	}

	@Override
	public PageInfo<Comments> commentsPage(CommentsSearch search) throws CodeException {
		// TODO Auto-generated method stub
		try {
			return PageHelper.startPage(search.getCurrent(), search.getRowMax()).doSelectPageInfo(()->mapper.select(search));
		}catch(Exception e) {
			throw new CodeException(5002);
		}
	}

	@Override
	public PageInfo<Comments> commentsPageAll(CommentsSearch search) throws CodeException {
		// TODO Auto-generated method stub
		try {
			return PageHelper.startPage(search.getCurrent(), search.getRowMax()).doSelectPageInfo(()->mapper.selectAll(search));
		}catch(Exception e) {
			throw new CodeException(5002);
		}
	}

	@Override
	public List<Comments> commentsList(String userName, Long commentCommentator) throws CodeException {
		// TODO Auto-generated method stub
		try {
			return mapper.selectByUserName(userName, commentCommentator);
		}catch(Exception e) {
			throw new CodeException(5002);
		}
	}

	@Override
	public Long commentsCountByCommentator(Long commentCommentator) throws CodeException {
		// TODO Auto-generated method stub
		try {
			return mapper.count(commentCommentator);
		}catch(Exception e) {
			throw new CodeException(5002);
		}
	}

	@Override
	public void commentsRemove(Long commentId) throws CodeException {
		// TODO Auto-generated method stub
		Comments comments=new Comments();
		comments.setCommentId(commentId);
		comments.setCommentDelete(true);
		mapper.updateByPrimaryKeySelective(comments);
	}

	@Override
	public List<Comments> commentsList1(Long commentCommentator) throws CodeException {
		return mapper.selectByCommmetCommentator1(commentCommentator);
	}

	@Override
	public Comments get(Long commentId) {
		// TODO Auto-generated method stub
		return mapper.selectByPrimaryKey(commentId);
	}

	@Override
	public void commentsRemove(Long commentId, boolean falg) throws CodeException {
		// TODO Auto-generated method stub
		Comments comments=new Comments();
		comments.setCommentId(commentId);
		comments.setCommentDelete(falg);
		mapper.updateByPrimaryKeySelective(comments);
	}

}
